// router.js

import Vue from 'vue'
import Router from 'vue-router'
import normalRoutes from './noraml'
import tool from '@/utils/tool'
Vue.use(Router)
let notFoundRoutes = []

// 生成资讯页相关路由,这部分路由是根据后台设置的专业，通过接口来生成的
function createDRoutes() {
  return []
  // 获取路由所需数据
  let professionData = {}
  
  if (typeof window === 'undefined') {
    professionData = process.professionData || {}
  } else {
    try {
      let state = window.__INITIAL_STATE__
      professionData = tool.decodeString(state.professionData) || {}
    } catch (error) {}
  }

  console.log('professionData',professionData);
  let { areaList=[], navList=[], professionList=[] } = professionData
  let newsRouterArr = professionList.reduce((sum, itemP) => {
    let navRouteArr = [],
      professionRouterArr = [],
      areaRouteArr = []
    // 专业首页路由
    let professionObj = {}
    professionObj.path = `/${itemP.path}/`
    professionObj.meta = {
      profession_id: itemP.profession_id,
      profession_name: itemP.profession_name,
      professionData
    }
    professionObj.component = itemP.path === 'kjsc' ? scHome : professionHome
    // 各专业对应的考试指南路由
    let professionGuideObj = {}
    professionGuideObj.path = `/${itemP.path}/guide/`
    professionGuideObj.meta = {
      profession_id: itemP.profession_id,
      profession_name: itemP.profession_name,
      profession_path: itemP.path
    }
    professionGuideObj.component = examGuide
    // 个考试指南每个tab详情指南
    let professionGuideTabObj = {}
    professionGuideTabObj.path = `/${itemP.path}/guideDetail/:id.html`
    professionGuideTabObj.meta = {
      profession_id: itemP.profession_id,
      profession_name: itemP.profession_name,
      profession_path: itemP.path
    }
    professionGuideTabObj.component = examGuide
    professionRouterArr.push(
      professionObj,
      professionGuideObj,
      professionGuideTabObj
    )
    // 生成专业栏目首页、详情页
    navRouteArr = navList.reduce((arr, itemN) => {
      // 专业栏目首页
      let navObj = {},
        detailObj = {}
      // 特殊处理，初级会计栏目和会计实操栏目重名，会计实操需要特殊处理policy和infos,course，当id为3和4,6的时候，不能加入会计实操拦路，直接return
      if (itemP.path === 'kjsc' && [3, 4, 6].includes(+itemN.id)) {
        return arr
      }
      navObj.path = `/${itemP.path}/${itemN.path}/`
      navObj.component = informationHome
      navObj.meta = {
        professionData,
        profession_id: itemP.profession_id,
        profession_name: itemP.profession_name,
        nav_id: itemN.id,
        nav_parent_id: itemN.parent_id,
        area_id: '1',
        crumbs: [
          { name: itemP.profession_name, path: `/${itemP.path}/` },
          { name: itemN.nav_name, path: `/${itemP.path}/${itemN.path}/` }
        ]
      }
      // 文章详情页
      detailObj.path = `/${itemP.path}/${itemN.path}_detail/:id.html`
      detailObj.component = informationDetail
      detailObj.meta = {
        professionData,
        profession_id: itemP.profession_id,
        profession_name: itemP.profession_name,
        nav_id: itemN.id,
        crumbs: [
          { name: itemP.profession_name, path: `/${itemP.path}/` },
          { name: itemN.nav_name, path: `/${itemP.path}/${itemN.path}/` },
          { name: '正文', path: '' }
        ]
      }
      arr.push(navObj, detailObj)
      return arr
    }, [])
    // 地区首页
    areaRouteArr = areaList.reduce((sum, itemA) => {
      let areaObj = {}
      areaObj.path = `/${itemP.path}/${itemA.path}/`
      areaObj.component = informationHome
      areaObj.meta = {
        professionData,
        profession_id: itemP.profession_id,
        profession_name: itemP.profession_name,
        nav_id: '1',
        nav_parent_id: '0',
        area_id: itemA.id,
        crumbs: [
          { name: itemP.profession_name, path: `/${itemP.path}/` },
          { name: itemA.area, path: `/${itemP.path}/${itemA.path}/` }
        ]
      }
      sum.push(areaObj)
      return sum
    }, [])
    sum.push(...professionRouterArr, ...navRouteArr, ...areaRouteArr)
    return sum
  }, [])
  return [...newsRouterArr, ...notFoundRoutes]
}
let routes = [...normalRoutes, ...createDRoutes()]
export function createRouter() {
  const router = new Router({
    mode: 'history', // SSR必须使用history模式
    scrollBehavior: () => ({ y: 0 }),
    routes: routes
  })
  router.beforeEach((to, from, next) => {
    // vue.ssr.js下新增了错误拦截，这里不需要再次拦截
    next()
  })
  return router
}
